package com.thon.controller.rest;

import com.thon.commons.persistence.Page;
import com.thon.controller.util.BaseController;
import com.thon.entity.college.College;
import com.thon.entity.system.Region;
import com.thon.service.system.RegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.List;

/**
 * Created by win on 2015/12/25.
 */
@Controller
@RequestMapping("/api/region")
public class RegionRestController extends BaseController {

    private static final String PAGE_SIZE = "5";

    @Autowired
    private RegionService regionService;

    //根据地区parentId查询它对应的所有子地区
    @RequestMapping(value = "/list/parent", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<HttpEntity> listRegions(
            @RequestParam(value = "parentId", defaultValue="1") int parentId,
            Model model) {

        List<Region> regions = regionService.listRegions(parentId);
        return new ResponseEntity(regions, HttpStatus.OK);
    }

    @RequestMapping(value = "/list/city", method = RequestMethod.GET)
    public ResponseEntity<HttpEntity> listCitys(
            @RequestParam(value = "parentId", defaultValue="10") int parentId,
            Model model) {

        List<Region> regions = regionService.listRegions(parentId);
        model.addAttribute("result", regions);
        return new ResponseEntity(model, HttpStatus.OK);
    }

    //根据地区level查询它对应的所有地区
    @RequestMapping(value = "/list/level")
    public ResponseEntity<HttpEntity> listRegionsByLevel(
            @RequestParam(value = "level") Byte level,
            @RequestParam(value="flag",defaultValue="1") int flag , Model model) {

        List<Region> regions = regionService.listRegionsByLevel(level);
        model.addAttribute("result", regions);
        if(flag==1){
            return new ResponseEntity(regions,HttpStatus.OK);
        }else{
            return new ResponseEntity(model,HttpStatus.OK);
        }
    }

    /**
     * @param level 区域等级
     * @param pageNo
     * @param pageSize
     * @param model
     * @return
     * 根据区域等级分页获取区域
     */
    @RequestMapping(value = "/find/level")
    public ResponseEntity<HttpEntity> listRegionPageByLevel(
            @RequestParam(value = "level") Byte level,
            @RequestParam(value = "p",defaultValue = "1") int pageNo,
            @RequestParam(value = "s",defaultValue = "8") int pageSize,Model model) {

        Page<Region> page = new Page<Region>();
        page = regionService.findRegion(pageNo, pageSize, level);
        return new ResponseEntity(page, HttpStatus.OK);
    }

    //将所有地区分页显示
    @RequestMapping(value = "/list")
    public ResponseEntity<HttpEntity> findRegionPage(
            @RequestParam(value = "p", required = false, defaultValue = "1") int p,
            @RequestParam(value = "s", required = false, defaultValue = "20") int s,
            Model model) {

        Page<Region> page = regionService.findRegionPage(p, s);
        return new ResponseEntity(page, HttpStatus.OK);
    }
    @RequestMapping(value = "/list/new")
    public ResponseEntity<HttpEntity> findRegionPages( @RequestParam(value = "p", required = false, defaultValue = "1") int p,
                                                       @RequestParam(value = "s", required = false, defaultValue = "20") int s,
                                                       Region region){
        Page<Region> page = new Page<Region>();
        page.setPageNo(p);
        page.setPageSize(s);
        page = regionService.findRegionPages(page, region);
        return new ResponseEntity(page, HttpStatus.OK);
    }
}
